summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_session_request.cpp
blob: a329e56900ac64b6072ae24b23ab1dffa67aebb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "core/hle/kernel/k_page_buffer.h"
#include "core/hle/kernel/k_session_request.h"

namespace Kernel {

Result KSessionRequest::SessionMappings::PushMap(VAddr client, VAddr server, size_t size,
                                                 KMemoryState state, size_t index) {
    // At most 15 buffers of each type (4-bit descriptor counts).
    ASSERT(index < ((1ul << 4) - 1) * 3);

    // Get the mapping.
    Mapping* mapping;
    if (index < NumStaticMappings) {
        mapping = std::addressof(m_static_mappings[index]);
    } else {
        // Allocate a page for the extra mappings.
        if (m_mappings == nullptr) {
            KPageBuffer* page_buffer = KPageBuffer::Allocate(m_kernel);
            R_UNLESS(page_buffer != nullptr, ResultOutOfMemory);

            m_mappings = reinterpret_cast<Mapping*>(page_buffer);
        }

        mapping = std::addressof(m_mappings[index - NumStaticMappings]);
    }

    // Set the mapping.
    mapping->Set(client, server, size, state);

    R_SUCCEED();
}

Result KSessionRequest::SessionMappings::PushSend(VAddr client, VAddr server, size_t size,
                                                  KMemoryState state) {
    ASSERT(m_num_recv == 0);
    ASSERT(m_num_exch == 0);
    R_RETURN(this->PushMap(client, server, size, state, m_num_send++));
}

Result KSessionRequest::SessionMappings::PushReceive(VAddr client, VAddr server, size_t size,
                                                     KMemoryState state) {
    ASSERT(m_num_exch == 0);
    R_RETURN(this->PushMap(client, server, size, state, m_num_send + m_num_recv++));
}

Result KSessionRequest::SessionMappings::PushExchange(VAddr client, VAddr server, size_t size,
                                                      KMemoryState state) {
    R_RETURN(this->PushMap(client, server, size, state, m_num_send + m_num_recv + m_num_exch++));
}

void KSessionRequest::SessionMappings::Finalize() {
    if (m_mappings) {
        KPageBuffer::Free(m_kernel, reinterpret_cast<KPageBuffer*>(m_mappings));
        m_mappings = nullptr;
    }
}

} // namespace Kernel